package com.fosss.band.controller;

import com.fosss.band.entity.Album;
import com.fosss.band.entity.Score;
import com.fosss.band.entity.User;
import com.fosss.band.entity.vo.AlbumQueryVo;
import com.fosss.band.service.AlbumService;
import com.fosss.band.service.ScoreService;
import com.fosss.band.utils.AuthRecord;
import com.fosss.band.utils.Result;
import com.fosss.band.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.fosss.band.utils.ResultCodeEnum.PERMISSION;

/**
 * @author: fosss
 * Date: 2024/1/2
 * Time: 20:10
 * Description:
 */
@Slf4j
@Api(tags = "专辑相关接口")
@RestController
@RequestMapping("/album")
public class AlbumController {

    @Resource
    private AlbumService albumService;
    @Resource
    private ScoreService scoreService;

    @ApiOperation("分页条件查询")
    @GetMapping("/getAlbumListPage/{userId}/{page}/{size}")
    public Result getAlbumListPage(@PathVariable("userId") Integer userId,
                                   @PathVariable("page") Integer page,
                                   @PathVariable("size") Integer size,
                                   AlbumQueryVo albumQueryVo) {
        log.info("专辑分页查询");
        return albumService.getAlbumListPage(userId, page, size, albumQueryVo);
    }

    @ApiOperation("删除专辑")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        User user = ThreadLocalUtil.get();
        int role = user.getRole();
        System.out.println(role);
        if (!AuthRecord.hasAuth(role, "/album/delete"))
            return Result.error().code(PERMISSION.getCode()).message(PERMISSION.getMessage());
        log.info("删除专辑：{}", id);
        return albumService.delete(id);
    }

    @ApiOperation("添加专辑")
    @PostMapping("/add")
    public Result add(@RequestBody Album album) {
        User user = ThreadLocalUtil.get();
        int role = user.getRole();
        System.out.println(role);
        if (!AuthRecord.hasAuth(role, "/album/add"))
            return Result.error().code(PERMISSION.getCode()).message(PERMISSION.getMessage());
        log.info("添加专辑：{}", album);
        return albumService.addAlbum(album);
    }

    @ApiOperation("根据id查询专辑")
    @GetMapping("/getAlbum/{id}")
    public Result getAlbum(@PathVariable("id") Integer id) {
        log.info("根据id查询专辑：{}", id);
        return albumService.getAlbum(id);
    }

    @ApiOperation("更新专辑")
    @PutMapping("/update")
    public Result update(@RequestBody Album album) {
        log.info("更新专辑:" + album);
        return albumService.update(album);
    }

    @ApiOperation("获取评分")
    @GetMapping("/getScore/{fanId}/{albumId}")
    public Result getScore(@PathVariable("fanId") Integer fanId, @PathVariable("albumId") Integer albumId) {
        log.info("获取评分 ");
        return Result.ok().data("row", scoreService.getByFanIdAndAlbumId(fanId, albumId));
    }

    @ApiOperation("给专辑打分")
    @PostMapping("/scoring")
    public Result scoring(@RequestBody Score score) {
        log.info("给专辑打分:{} ", score);
        return albumService.scoring(score);
    }
}

